package org.origin.ferret.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.enums.result.RS;
import org.origin.centre.model.result.R;
import org.origin.centre.utils.UserUtil;
import org.origin.ferret.system.mapstruct.SysUserMapstruct;
import org.origin.ferret.system.mapstruct.SysUsersMapstruct;
import org.origin.ferret.system.model.vo.menu.SysMenuVo;
import org.origin.ferret.system.model.vo.user.SysUserVo;
import org.origin.ferret.system.model.vo.user.SysUsersVo;
import org.origin.ferret.system.service.SysGrantService;
import org.origin.ferret.system.service.SysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户中心-接口
 *
 * @author ferret
 * @version 2024-05-08
 */
@RestController
@Tag(name = "用户中心-接口")
@RequiredArgsConstructor
@RequestMapping("${origin.api:}/sys/users")
public class SysUsersController {
    private final SysUserService userService;
    private final SysGrantService grantService;
    private final SysUsersMapstruct usersMapstruct;

    @GetMapping("info")
    @Operation(summary = "1.用户信息", description = "用户信息")
    public R<SysUserVo> info() {
        String userId = UserUtil.getUserId();
        SysUserVo userVo = this.userService.query(new XQueryByIdDto(userId));
        List<SysMenuVo> menusByUser = this.grantService.getMenusByUser(userId, UserUtil.isAdmin());
        SysUsersVo usersVo = this.usersMapstruct.toUsers(userVo, menusByUser);
        return R.ok(RS.OK, usersVo);
    }
}
